package com.steamedfish.auctionitemserver.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.steamedfish.auctionitemserver.entiry.AuctionItem;
import com.steamedfish.auctionitemserver.entiry.AuctionItemImage;
import com.steamedfish.auctionitemserver.model.AuctionItemModel;
import com.steamedfish.auctionitemserver.service.AuctionItemImageService;
import com.steamedfish.auctionitemserver.service.AuctionItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

public class TestController {

    @Autowired
    private static AuctionItemService auctionItemService;

    @Autowired
    private static AuctionItemImageService auctionItemImageService;


    public static List<AuctionItemModel> searchAuctionItem(QueryWrapper queryWrapper) {

        //根据用户id查询发布的信息
        List<AuctionItem> auctionItems = auctionItemService.list(queryWrapper);

        //根据拍品信息查询图片信息
        List<AuctionItemModel> auctionItemModels = new ArrayList<>();
        auctionItems.forEach(e -> {
            AuctionItemModel auctionItemModel = new AuctionItemModel();
            BeanUtils.copyProperties(e, auctionItemModel);

            //查询图片信息
            QueryWrapper queryWrapperForImage = new QueryWrapper();
            queryWrapperForImage.eq("item_image_id", e.getItemImageId());
            List<AuctionItemImage> images = auctionItemImageService.list(queryWrapperForImage);

            String[] urlList = new String[images.size()];
            for (int i = 0; i < images.size(); i++) {
                urlList[i] = images.get(i).getImage();
            }
            auctionItemModel.setUrls(urlList);
            auctionItemModels.add(auctionItemModel);
        });

        //返回拍品数据信息
        System.out.println(JsonUtils.toJson(auctionItemModels));


        return auctionItemModels;
    }

}